package com.ljfiction.book.activitys;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidkun.xtablayout.XTabLayout;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.ljfiction.book.Baner.Fansrankbean;
import com.ljfiction.book.R;
import com.ljfiction.book.adapter.IndentDetailAdapter;
import com.ljfiction.book.fragment.FensiAllRankfragment;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.http.UserManageBean;
import com.ljfiction.book.tools.SettingManager;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import jp.wasabeef.glide.transformations.CropCircleTransformation;
import okhttp3.Call;
import okhttp3.Response;

public class FansRankActivity extends BaseActivity {
    @BindView(R.id.back_fensirank)
    ImageView backFensirank;
    @BindView(R.id.fensiranktable)
    XTabLayout fensiranktable;
    @BindView(R.id.fensirankviewpager)
    ViewPager fensirankviewpager;
    @BindView(R.id.myuserimage_fensrank)
    ImageView myuserimageFensrank;
    @BindView(R.id.myfensinum_fansrank)
    TextView myfensinumFansrank;
    @BindView(R.id.paiming_fansrank)
    TextView paimingFansrank;
    @BindView(R.id.howmach_fansrank)
    TextView howmachFansrank;
    @BindView(R.id.paimingname)
    TextView paimingname;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private String tabTitles[] = new String[]{"总榜", "周榜"};
    private String bookId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fans_rank);
        ButterKnife.bind(this);
        bookId = getIntent().getStringExtra("bookId");
        intview();
    }

    private void intData() {
        OkHttpUtils.get(HttpManager.IP + "/fans-bookFansMyRanking/" + bookId)

                .execute(new StringCallback() {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson = new Gson();
                        Fansrankbean fansrankbean = gson.fromJson(s, Fansrankbean.class);
                        if (fansrankbean.getReturnCode() == 200) {
                            if (fansrankbean.getData().getBookFansRanKing().getCounts() == 0) {
                                myfensinumFansrank.setText("当前粉丝值 0");
                                paimingFansrank.setVisibility(View.VISIBLE);
                                paimingname.setVisibility(View.VISIBLE);
                                paimingFansrank.setText("未入榜");
                                paimingFansrank.setTextColor(Color.parseColor("#f77583"));
                                howmachFansrank.setText("获得1粉丝值即可入榜");
                            }else {
                                myfensinumFansrank.setText("当前粉丝值  "+fansrankbean.getData().getBookFansInfo().getFansCount());
                                paimingFansrank.setText(String.valueOf(fansrankbean.getData().getBookFansRanKing().getCounts()));
                                paimingFansrank.setTextColor(Color.parseColor("#333333"));
                                howmachFansrank.setText("获得"+(fansrankbean.getData().getBookFansRanKing().getFansCount()-fansrankbean.getData().getBookFansInfo().getFansCount())+"1粉丝值即可入榜");
                            }
                            Glide.with(FansRankActivity.this).load(UserManageBean.userBean.getUserHeadPortraitURL()).bitmapTransform(new CropCircleTransformation(FansRankActivity.this)).into(myuserimageFensrank);
                        }else if (fansrankbean.getReturnCode()==400){
                            toNoLogin();
                        }else {
                            showToast(fansrankbean.getMsg());
                        }
                    }
                });
    }

    private void intview() {
        fragments.add(new FensiAllRankfragment().newInstance(bookId, 0));
        fragments.add(new FensiAllRankfragment().newInstance(bookId, 1));
        IndentDetailAdapter adapter = new IndentDetailAdapter(getSupportFragmentManager(), tabTitles, fragments);
        fensirankviewpager.setAdapter(adapter);
        fensirankviewpager.setOverScrollMode(View.OVER_SCROLL_NEVER);
        fensiranktable.setupWithViewPager(fensirankviewpager);
        if (UserManageBean.isLogin()) {
            intData();
        } else {
            toNoLogin();
        }
    }

    private void toNoLogin() {
        myuserimageFensrank.setImageResource(R.drawable.persenmoren);
        myfensinumFansrank.setText("未登录");
        paimingname.setVisibility(View.GONE);
        paimingFansrank.setVisibility(View.GONE);
        howmachFansrank.setText("点击头像登陆查看自己的排名情况");
    }

    @OnClick({R.id.explanation_fansrank, R.id.back_fensirank, R.id.myuserimage_fensrank})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_fensirank:
                goBack();
                break;

            case R.id.explanation_fansrank:
                Intent intent=new Intent(FansRankActivity.this,FansShuomingActivity.class);
                startActivity(intent);
                break;

            case R.id.myuserimage_fensrank:
                UserManageBean.isLogin(FansRankActivity.this);
                break;
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (UserManageBean.isLogin()) {
            intData();
        } else {
            toNoLogin();
        }
    }
}
